package com.haixiaoke.saas.repository.util.timePair;

import cn.hutool.core.date.DateUtil;
import com.haixiaoke.saas.repository.vo.WeeklyTimeVo;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.Month;
import java.util.Calendar;
import java.util.Date;

/**
 * 获取本月对应周数的范围
 */
@Component
public class WeeklyTimeUtil {

    public WeeklyTimeVo getWeeklyTime(Integer Number) {
        WeeklyTimeVo weeklyTimeVo = new WeeklyTimeVo();
        Calendar calendar = Calendar.getInstance();
        int weekNumber = Number - 1; // 指定的周数
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); // 设置每周的第一天为星期天
        calendar.set(Calendar.DAY_OF_WEEK_IN_MONTH, weekNumber); // 设置指定周数的星期天
        Date startOfMonth = calendar.getTime(); // 获取本月的第一天
        calendar.add(Calendar.DATE, 6); // 将日期加6天，得到本周的星期六
        Date endOfWeek = calendar.getTime(); // 获取本周的星期六
        Date date = new Date();//获取当前时间
        int thisMonth = date.getMonth() + 1; //获取当前时间月份
        int startMonth = startOfMonth.getMonth() + 1;
        int endWeek = endOfWeek.getMonth() + 1;
        String start = DateUtil.formatDate(startOfMonth);
        String end = DateUtil.formatDate(endOfWeek);
        if (thisMonth != startMonth) { // 如果开始时间早于本月第一天
            start = getDayOne(); // 将开始时间设置为本月的第一天
        }
        if (thisMonth != endWeek) { // 如果结束时间晚于本月最后一天
            end = getLastDay(); // 将结束时间设置为本月的最后一天
        }
        weeklyTimeVo.setStartDate(start);
        weeklyTimeVo.setEndDate(end);
        return weeklyTimeVo;
    }

    /**
     * 获取本月第一天
     *
     * @return
     */
    private static String getDayOne() {
        // 获取当前日期的Calendar实例
        Calendar calendar = Calendar.getInstance();
        // 设置为本月的第一天
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return DateUtil.formatDate(calendar.getTime());
    }

    /**
     * 获取本月最后一天
     *
     * @return
     */
    private static String getLastDay() {
        // 获取当前日期的Calendar实例
        Calendar calendar = Calendar.getInstance();
        // 设置为下个月的第一天
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        // 获取本月最后一天的日期
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        return DateUtil.formatDate(calendar.getTime());
    }

    /**
     * 获取本月有多少周
     *
     * @return
     */
    public Integer getWeeklyNumber() {
        Calendar calendar = Calendar.getInstance();
        return calendar.getActualMaximum(Calendar.WEEK_OF_MONTH); // 获取本月的周数
    }

    /**
     * 获取当前时间是本月第几周
     *
     * @return
     */
    public Integer getWeek() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.WEEK_OF_MONTH); // 获取本月的周数
    }

    /**
     * 获取今年不同季度对应的时间区间
     */
    public WeeklyTimeVo getQuarter(Integer quarter) {
        WeeklyTimeVo weeklyTimeVo = new WeeklyTimeVo();
        // 获取当前年份
        int currentYear = LocalDate.now().getYear();

        // 计算季度的起始月份和结束月份
        Month startMonth = Month.of((quarter - 1) * 3 + 1);
        Month endMonth = startMonth.plus(2);

        // 构造时间区间的起始日期和结束日期
        LocalDate startDate = LocalDate.of(currentYear, startMonth, 1);
        LocalDate endDate = LocalDate.of(currentYear, endMonth, endMonth.length(startDate.isLeapYear()));
        String strStartDate = String.valueOf(startDate);
        String strEndDate = String.valueOf(endDate);
        weeklyTimeVo.setStartDate(strStartDate);
        weeklyTimeVo.setEndDate(strEndDate);
        return weeklyTimeVo;
    }

    public static void main(String[] args) {
        WeeklyTimeUtil weeklyTimeUtil = new WeeklyTimeUtil();
        WeeklyTimeVo weeklyTime = weeklyTimeUtil.getWeeklyTime(2);
        String startDate = weeklyTime.getStartDate();
        System.out.println("startDate = " + startDate);
        String endDate = weeklyTime.getEndDate();
        System.out.println("endDate = " + endDate);

        Integer weeklyNumber = weeklyTimeUtil.getWeeklyNumber();
        System.out.println("weeklyNumber = " + weeklyNumber);

        Integer week = weeklyTimeUtil.getWeek();
        System.out.println("week = " + week);

        WeeklyTimeVo quarter = weeklyTimeUtil.getQuarter(4);
        String startDate1 = quarter.getStartDate();
        System.out.println("startDate1 = " + startDate1);
        String endDate1 = quarter.getEndDate();
        System.out.println("endDate1 = " + endDate1);

    }
}


